package com.hulk.boot.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import com.hulk.boot.common.tool.jedis.JedisSentinelPoolFactoryBean;
import com.hulk.boot.config.condition.ProfileSentinelCondition;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

/** 
* @author 作者  cmt
* @version 创建时间：2016年5月27日 下午4:53:52 
* 类说明 
*/



@Configuration
@Conditional(value = {ProfileSentinelCondition.class})
public class JedisSentinelConfig {
	
	
	@Value(value = "${spring.redis.database}")
	private Integer springRedisDatabase;
	@Value(value = "${spring.redis.password}")
	private String springRedisPassword;
	@Value(value = "${spring.redis.timeout}")
	private Integer springRedisTimeout;
	
	
//	@Value(value = "${spring.redis.host}")
//	private String springRedisHost;
//	@Value(value = "${spring.redis.port}")
//	private String springRedisPort;
	
	@Value(value = "${spring.redis.sentinel.master}")
	private String springRedisSentinelMaster;
	@Value(value = "${spring.redis.sentinel.nodes}")
	private String springRedisSentinelNodes;
	
	@Value(value = "${spring.redis.pool.max-idle}")
	private Integer springRedisPoolMax_idle;
	@Value(value = "${spring.redis.pool.min-idle}")
	private Integer springRedisPoolMin_idle;
	@Value(value = "${spring.redis.pool.max-active}")
	private Integer springRedisPoolMax_active;
	@Value(value = "${spring.redis.pool.max-wait}")
	private Long springRedisPoolMax_wait;;
	
	
	@Bean(name = "jedisSentinelPoolFactoryBean")
	public JedisSentinelPoolFactoryBean jedisSentinelPoolFactoryBean() {
		JedisSentinelPoolFactoryBean jedisSentinelPoolFactoryBean = new JedisSentinelPoolFactoryBean();
		JedisPoolConfig  jedisPoolConfig =new	redis.clients.jedis.JedisPoolConfig();
		jedisPoolConfig.setMinIdle(springRedisPoolMin_idle);
		jedisPoolConfig.setMaxIdle(springRedisPoolMax_idle);
		jedisPoolConfig.setMaxTotal(springRedisPoolMax_active);
		jedisPoolConfig.setMaxWaitMillis(springRedisPoolMax_wait);
		jedisSentinelPoolFactoryBean.setGenericObjectPoolConfig(jedisPoolConfig);
		jedisSentinelPoolFactoryBean.setTimeout(springRedisTimeout);
		jedisSentinelPoolFactoryBean.setDatabase(springRedisDatabase);
		jedisSentinelPoolFactoryBean.setPassword(springRedisPassword);
		jedisSentinelPoolFactoryBean.setSentinelMasterName(springRedisSentinelMaster);
		jedisSentinelPoolFactoryBean.setSentinelNodes(springRedisSentinelNodes);
		return jedisSentinelPoolFactoryBean;
	}
	
//	@Conditional(value = {ProfileSentinelCondition.class})
//	@Bean(name = "jedisPoolManager")
//	@Qualifier("jedisPoolManager")
//	public JedisSentinelPoolBase jedisSentinelPoolManager(JedisSentinelPoolFactoryBean bean) {
//		JedisSentinelPoolBase jedisSentinelPoolTemplate = new JedisSentinelPoolBase(bean.getJedisSentinelPool()) ;
//		return jedisSentinelPoolTemplate;
//	}
	
	
	
	@Bean(name = "jedisPoolManager")
	@Qualifier("jedisPoolManager")
	public JedisSentinelPool jedisSentinelPoolManager(JedisSentinelPoolFactoryBean bean) {
		JedisSentinelPool jedisSentinelPool = bean.getObject();
		return jedisSentinelPool;
	}
	

}
